
var prefix = "/jzweb/jazUserOrder";
$(function() {
	load();
});

let pages = 1;
function load() {
	layui.use(['table','laydate'], function(){
		var table = layui.table,  laydate = layui.laydate,
			$ = layui.$;
		laydate.render({
			elem: '#ID-laydate-range',
			range: ['#ID-laydate-start-date', '#ID-laydate-end-date'],
			rangeLinked:true,
			shortcuts: [
				{
					text: "上个月",
					value: function(){
						var date = new Date();
						var year = date.getFullYear();
						var month = date.getMonth();
						return [
							new Date(year, month - 1, 1),
							new Date(year, month, 0)
						];
					}
				},{
					text: "这个月",
					value: function(){
						var date = new Date();
						var year = date.getFullYear();
						var month = date.getMonth();
						return [
							new Date(year, month, 1),
							new Date(year, month + 1, 0)
						];
					}
				},
				{
					text: "下个月",
					value: function(){
						var date = new Date();
						var year = date.getFullYear();
						var month = date.getMonth();
						return [
							new Date(year, month + 1, 1),
							new Date(year, month + 2, 0)
						];
					}
				}
			]
		});
		// 创建渲染实例
		table.render({
			elem: '#exampleTable'
			,id:'exampleTable'
			// prefix
			,url: prefix + "/list" // 此处为静态模拟数据，实际使用时需换成真实接口
			,cellMinWidth: 80
			, page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
				layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
				, curr: pages //设定初始在第 5 页
				, groups: 5 //只显示 1 个连续页码
				, limit : 15
				, limits : [15,30,50,100,200]
			}
			,request:{
				pageName: 'current' //页码的参数名称，默认：page
				,limitName: 'size'
			}
			,where: {
				name: $("#name").val().trim() ,
				status: $("#type").val()
			}
			,defaultToolbar: false
			,loading: false
			,cols: [[
				// {
				// 	type: 'checkbox', layData: {type: 'checkbox'}
				// },
				{
					field : 'id',
					title : '订单编号'
				},
				{
					field : 'username',
					title : '创建人'
				},
				{
					field: 'mobile',
					title: '联系方式'
				},
				{
					field : 'createTime',
					title : '创建时间'
				},
				{
					field : 'status',
					title : '订单状态',
					templet: function (d) {
						switch (d.status) {
							case '0':
								return '<span class="layui-btn layui-btn-sm layui-bg-blue">待开始</span>'
							case '1':
								return '<span class="layui-btn layui-btn-sm layui-bg-orange">进行中</span>'
							case '2':
								return '<span class="layui-btn layui-btn-sm">已完成</span>'
							case '3':
								return '<span class="layui-btn layui-btn-sm layui-bg-red">已取消</span>'
							default:
								return '<span>-</span>'
						}
					}
				},
				{
					field : 'remark',
					title : '订单备注',
					align : 'center',
				},
				{
					title : '操作',
					field : 'id',
					align : 'center',
					templet : function(d) {
						var e = `<a  class="btn btn-primary btn-sm " href="#" mce_href="#" title="详情" onclick="lookInfo(${ d.id + 'n'},${d.initiateId + 'n'},${d.signingId +'n'})"><i class="fa fa-send"></i></a> `;
						return e ;
					}
				}
			]]
			,response: {
				statusCode: 0 //重新规定成功的状态码为 200，table 组件默认为 0
			}
			,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
				console.log(res,'45656465')
				if( res.code != 0 ){

					return false;
				}
				return {
					"code": res.code, //解析接口状态
					"msg": res.msg, //解析提示文本
					"count": res.data.total, //解析数据长度
					"data": res.data.records //解析数据列表
				};
			}
			,jump: function (obj, first) {
				//首次不执行
				if (!first) {
					pages = obj.curr
				}
			}
			,error: function(res, msg){
				console.log(res, msg)
			}
			,done: function (res){
				layer.close()
				hindLoading()
			}
		});

		var $ = layui.$, active = {
			reload: function(){
				//执行重载
				table.reload('exampleTable', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
					,where: {
						nickname: $('#nickname').val(),
						status: $('#type').val()
					}
				});
			}
		};

		/**
		 * 搜索事件
		 */
		$('.demoTable .layui-btn').on('click', function(){
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

		/**
		 * 导出
		 */
		$("#export").click(function (){
			layer.confirm('确认要导出订单列表吗？', {
				btn: ['确认', '取消'] //可以无限个按钮
			},async function(index, layero){
				window.location.href = `${prefix}/downloadImportStu?status=${$('#type').val()}`
				layer.closeAll();
			}, function(index){
			});

		})
		table.on('checkbox(myTable)', function (obj) {
			console.log('123132')
		})


		//触发表格复选框选择
		table.on('checkbox(test)', function(obj){
			console.log(obj,'asd')
		});

		//触发表格单选框选择
		table.on('radio(test)', function(obj){
			console.log(obj,'asd')
		});

	});

}

/**
 *
 * @param id  订单id
 * @param initiateId 订单发起人id
 * @param signingId 订单签约人id
 */
function lookInfo(id,initiateId,signingId){
	const index = layer.open({
		type: 2,
		title:'订单详情',
		// maxmin: true,
		shadeClose: false,
		resize: false,
		area:['800px','500px'],
		content: prefix + `/info?id=${id}&initiateId=${initiateId}&signingId=${signingId}`,
	});
	layer.full(index);
}

/**
 * 导出请求
 */
function derive(data) {
	return $.ajax({
		type : "POST",
		url : prefix + "/downloadImportStu",
		responseType: 'blob',
		headers: {
                'Content-Type': 'application/json'
		},
		data : {
			status: data
		},
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(res) {
			const blob = new Blob([res]);
			const fileName = '统计.xlsx';
			const elink = document.createElement('a');
			elink.download = fileName;
			elink.style.display = 'none';
			elink.href = URL.createObjectURL(blob);
			document.body.appendChild(elink);
			elink.click();
			URL.revokeObjectURL(elink.href); // 释放URL 对象
			document.body.removeChild(elink);

		}
	});
}
